<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/10
 * Time: 8:50
 */

namespace common\business\mdm;


use common\business\Handler;
use common\business\ifs\CrudInterface;
use common\business\ifs\LogInterface;
use common\consts\CommonResources;
use common\models\Log;
use common\result\ActionResult;
use yii\helpers\Json;
use Yii;

abstract class MdmCrud extends Handler implements CrudInterface, LogInterface
{
    /**
     * @param $log Log
     * @return bool
     */
    public function persistLog($log)
    {
        $actionIsSuccess = $log->save();
        if (!$actionIsSuccess) {
            $dbErrorMsg = Json::encode($log->getErrors());
            $this->addError('persistLog', $dbErrorMsg);
            $log->memo = $log->memo . ' | ' . CommonResources::ACTION_FAIL_MSG('Persist', 'Log', $dbErrorMsg);
            Yii::error(CommonResources::ACTION_FAIL_MSG('Save', 'Log', $log->memo));
        }
        return $actionIsSuccess;
    }

    /**
     * @param $obj
     * @param $log Log
     * @return ActionResult
     */
    abstract public function updateWithLog($obj, $log);
}